import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { ProductService } from './product.service';
import { CreateProductDto, UpdateProductDto, QueryProductDto } from './dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { Public } from '../auth/decorators/public.decorator';
import { PermissionsGuard } from 'src/common/guards/permissions.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator';

/**
 * 商品管理控制器
 */
@ApiTags('商品管理')
@Controller('products')
@UseGuards(JwtAuthGuard)
export class ProductController {
  constructor(private readonly productService: ProductService) {}

  /**
   * 创建商品
   *
   * 需要 product:create 权限
   */
  @Post()
  @UseGuards(PermissionsGuard)
  @Permissions('product:create')
  @ApiBearerAuth()
  @ApiOperation({ summary: '创建商品' })
  async create(@Body() createProductDto: CreateProductDto) {
    return await this.productService.create(createProductDto);
  }

  /**
   * 获取商品列表（分页）
   *
   * 公开接口，任何人都可以访问
   */
  @Get()
  @Public()
  @ApiOperation({ summary: '获取商品列表' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'keyword', required: false, type: String })
  @ApiQuery({ name: 'categoryId', required: false, type: Number })
  @ApiQuery({
    name: 'status',
    required: false,
    enum: ['draft', 'published', 'offline'],
  })
  async findAll(@Query() query: QueryProductDto) {
    return await this.productService.findAll(query);
  }

  /**
   * 获取热门商品
   *
   * 公开接口
   */
  @Get('hot')
  @Public()
  @ApiOperation({ summary: '获取热门商品' })
  @ApiQuery({ name: 'limit', required: false, type: Number, default: 10 })
  async getHotProducts(@Query('limit') limit: number = 10) {
    return await this.productService.getHotProducts(limit);
  }

  /**
   * 获取推荐商品
   *
   * 公开接口
   */
  @Get('featured')
  @Public()
  @ApiOperation({ summary: '获取推荐商品' })
  @ApiQuery({ name: 'limit', required: false, type: Number, default: 10 })
  async getFeaturedProducts(@Query('limit') limit: number = 10) {
    return await this.productService.getFeaturedProducts(limit);
  }

  /**
   * 获取商品详情
   *
   * 公开接口
   */
  @Get(':id')
  @Public()
  @ApiOperation({ summary: '获取商品详情' })
  async findOne(@Param('id') id: string) {
    // 增加浏览量
    await this.productService.incrementViews(id);
    return await this.productService.findOne(id);
  }

  /**
   * 更新商品
   *
   * 需要 product:update 权限
   */
  @Put(':id')
  @UseGuards(PermissionsGuard)
  @Permissions('product:update')
  @ApiBearerAuth()
  @ApiOperation({ summary: '更新商品' })
  async update(
    @Param('id') id: string,
    @Body() updateProductDto: UpdateProductDto,
  ) {
    return await this.productService.update(id, updateProductDto);
  }

  /**
   * 删除商品
   *
   * 需要 product:delete 权限
   */
  @Delete(':id')
  @UseGuards(PermissionsGuard)
  @Permissions('product:delete')
  @ApiBearerAuth()
  @ApiOperation({ summary: '删除商品' })
  async remove(@Param('id') id: string) {
    return await this.productService.remove(id);
  }

  /**
   * 发布商品
   *
   * 需要 product:update 权限
   */
  @Put(':id/publish')
  @UseGuards(PermissionsGuard)
  @Permissions('product:update')
  @ApiBearerAuth()
  @ApiOperation({ summary: '发布商品' })
  async publish(@Param('id') id: string) {
    return await this.productService.publish(id);
  }

  /**
   * 下架商品
   *
   * 需要 product:update 权限
   */
  @Put(':id/offline')
  @UseGuards(PermissionsGuard)
  @Permissions('product:update')
  @ApiBearerAuth()
  @ApiOperation({ summary: '下架商品' })
  async offline(@Param('id') id: string) {
    return await this.productService.offline(id);
  }

  /**
   * 下载商品
   *
   * 需要登录
   */
  @Post(':id/download')
  @ApiBearerAuth()
  @ApiOperation({ summary: '下载商品' })
  async download(@Param('id') id: string) {
    await this.productService.incrementDownloads(id);
    const product = await this.productService.findOne(id);

    return {
      fileUrl: product.fileUrl,
      fileName: product.title,
      fileSize: product.fileSize,
    };
  }
}
